﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 运算符重载
{
    class ComplexNumber
    {
        public double Real { get;private set; }
        public double Imaginary { get;private set; }

        public ComplexNumber(double r, double i)
        {
            Real = r;
            Imaginary = i;
        }

        public override string ToString()
        {
            return string.Format("({0}{1}{2}i)", Real, ((Imaginary > 0) ? "+" : "-"), Math.Abs(Imaginary));
        }

        public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b)
        {
            return new ComplexNumber(a.Real + b.Real, a.Imaginary + b.Imaginary);
        }

        public static ComplexNumber operator -(ComplexNumber a, ComplexNumber b)
        {
            return new ComplexNumber(a.Real - b.Real, a.Imaginary - b.Imaginary);
        }

        public static ComplexNumber operator *(ComplexNumber a, ComplexNumber b)
        {
            return new ComplexNumber((a.Real * b.Real - a.Imaginary * b.Imaginary), (a.Imaginary * b.Real + a.Real * b.Imaginary));
        }
    }//end class ComplexNumber
}
